global google: env google

global work $google\Payments\Public

cd "$work"

global method logit
global control age tenure eduyr
global sumvar choice local age tenure eduyr riskaversion

global sample inconsistent==0
global option vce(cl cashier)
global outopt plain coljust(lc) var se starloc(1) starlevels(10 5 1) bdec(3) ///
	summstat(N \ r2_p) summtitles(Obs \ Pseudo R2)
global outopt2 plain coljust(lc) var se starloc(1) starlevels(10 5 1) bdec(3) ///
	summstat(N \ N_clust  \ r2_p) summtitles(Obs \ Cashiers  \ Pseudo R2)

*** Figure 2 Wage differential for accepting cash payment ***
use "$work\Stata\survey-public.dta" , clear
*** Correct for inattention (Mas and Pallais,2017) ***
** Stipulate error = proportion of cashiers who responded inconsistently in the inattention check question
egen tag1 = tag(cashier)
sum inconsistent if tag1==1 
gen error = r(mean)
** Define likelihood functions for error-corrected maximum likelihood logit model
capture program drop mylogit_mle
program define mylogit_mle
args lnf Xb
	replace `lnf' = ln(invlogit(`Xb')*(1-error) + (1-invlogit(`Xb'))*(error)) if $ML_y1==1
	replace `lnf' =  ln(invlogit(`Xb')*(error) + (1-invlogit(`Xb'))*(1-error)) if $ML_y1==0
end	

egen tag = tag(relwage)
bysort relwage: egen meanchoice = mean(choice)

/* Inattention correction (Mas and Pallais,2017):
Inattentive cashiers are equally likely to choose between cash and card. 
If  2*x of them are inattentive, half of them (x) would choose the cash option by chance.  
Therefore, 
Proportion who choose cash = (Proportion who prefer cash)*(1 - x) + (1-Proportion who prefer cash)*x.
Rearranging,
Proportion who prefer cash = (Proportion who choose cash - x) / (1 - 2*x)
*/
gen meanchoice1 = (meanchoice - error)/(1-2*error)
replace meanchoice = . if tag != 1

ml model lf mylogit_mle (choice=relwage), vce(robust)
ml max, iterate(100)
predict lfit
gen logit_fit = invlogit(lfit)

graph twoway (line logit_fit relwage, lcolor(navy) lwidth(medthick)) ///
  (scatter meanchoice relwage, mlcolor(navy) mfcolor(none)) ///
  (scatter meanchoice1 relwage, mcolor(navy)) , ///
	xlab(-40(5)40) ylabel(0(0.2)1) xtitle("Wage premium for cash payment" "($ per month)") ///
	ytitle("Fraction choosing cash") ///
	legend(label(1 "Maximum likelihood logit") label(3 "Corrected for inattention") ///
	label(2 "Uncorrected for inattention") order (2 3 1)) 
	
graph export "$work\Figures\figure2.png" , replace as(png) width(1200) height(900)

** Figure 3 Reasons for preferring card***
/*1: Risk - avoid mistakes by cash
2: Avoid conflicts with customers
3: Less effort / fast / convenient 
4: Less stress / no counting
5: Others */

use "$work\Stata\survey-public.dta" , clear
 
gen byte reason_risk = strpos(reason_card, "short") | strpos(reason_card, "reliable") ///
  | strpos(reason_card, "mistakes") | strpos(reason_card, "mistake") ///
  | strpos(reason_card, "mishandle") | strpos(reason_card, "safe") ///
  | strpos(reason_card, "accurate") | strpos(reason_card, "tally") ///
  | strpos(reason_card, "balance") | strpos(reason_card, "boss") ///
  | strpos(reason_card, "risk") | strpos(reason_card, "wrongly")
gen byte reason_conflict = strpos(reason_card, "customer") ///
  | strpos(reason_card, "conflict")  | strpos(reason_card, "conflicts")
gen byte reason_convenience = strpos(reason_card, "convenient") ///
  | strpos(reason_card, "fast") | strpos(reason_card, "easy") ///
  | strpos(reason_card, "wait")| strpos(reason_card, "easier")
gen byte reason_stress = strpos(reason_card, "count") ///
  | strpos(reason_card, "stress") | strpos(reason_card, "change") 
gen byte reason_other = strpos(reason_card, "dirty") ///
  | strpos(reason_card, "fasionable") 
replace reason_other = 1 if (reason_card!="" & reason_risk==0 & reason_conflict==0 & reason_convenience==0 & reason_stress==0)

collapse (mean) reason_risk reason_conflict reason_convenience reason_stress reason_other , by(cashier)

foreach var in reason_risk reason_conflict reason_convenience reason_stress reason_other {
  replace `var' = 1 if `var'>0 
  }

collapse (sum) reason_risk reason_conflict reason_convenience reason_stress reason_other
gen id=1
reshape long reason_, i(id) j(reas) string
drop id
rename reas reason
rename reason_ obs

replace reason = "Avoid conflict" if reason == "conflict"
replace reason = "Convenient" if reason == "convenience"
replace reason = "Others" if reason == "other"
replace reason = "Avoid risk" if reason == "risk"
replace reason = "Reduce stress" if reason == "stress"

egen total=total(obs)
gen per_obs=obs/total * 100
graph bar per_obs, over(reason, sort(1) descending) ytitle(% of cashiers) blabel(bar, format(%4.1f))
graph export "$work\Figures\figure3.png" , replace as(png) width(1200) height(900)

*** Table 3. Summary statistics ***/
use "$work\Stata\survey-public.dta" , clear 
keep if $sample
su $sumvar, detail
outreg2 using "$work\Stata-out\cashiersurvey.doc" , replace sum(log) wide label ///
	eqkeep(N mean sd min max) keep($sumvar) sortvar($sumvar) ///
	title("Table 3. Summary statistics")

collapse (first) mode_pref, by(cashier) 
gen cash_pref=(mode_pref==0)
outreg2 using "$work\Stata-out\cashiersurvey.doc" , sum(log) wide label ///
	eqkeep(N mean sd min max) keep(cash_pref) sortvar(cash_pref $sumvar)

*** Table 4. Cash preference ***
use "$work\Stata\survey-public.dta" , clear
replace relwage = relwage / 10
label var risk "Risk tolerance"
label var risk_miss "Risk tolerance missing"

//2 variables indicating questionnaire types: from -3.5 to -0.5, or -4 to -1, and from 0.5 to 3.5, or 1 to 4
bysort cashier: egen minrelwage=min(relwage)
bysort cashier: egen maxrelwage=max(relwage)
gen type_card=0 
replace type_card=1 if minrelwage==-4
replace type_card=2 if minrelwage==-3.5
gen type_cash=0 
replace type_cash=1 if maxrelwage==4
replace type_cash=2 if maxrelwage==3.5
	
//Sample: prefer card
outreg, clear(table)
xi: $method choice relwage i.store i.type_card i.type_cash if $sample & mode_pref==1 & relwage>0, $option
outreg , $outopt2 merge(table) keep(relwage) ///
	ctitle("VARIABLES", "Differential wage")

xi: $method choice relwage local i.store i.type_card i.type_cash if $sample & mode_pref==1 & relwage>0, $option
outreg , $outopt2 merge(table) keep(relwage local) ///
	ctitle("VARIABLES", "Local")		
	
xi: $method choice relwage riskaversion risk_miss i.store i.type_card i.type_cash if $sample & mode_pref==1 & relwage>0, $option
outreg , $outopt2 merge(table) keep(relwage riskaversion risk_miss) ///
	ctitle("VARIABLES", "Risk aversion")	

xi: $method choice relwage local riskaversion risk_miss $control i.store i.type_card i.type_cash ///
	if $sample & mode_pref==1 & relwage>0, $option
outreg , $outopt2 merge(table) keep(relwage local riskaversion risk_miss $control) ///
	ctitle("VARIABLES", "All")
	
sum age, detail
gen age_h = (age >= r(p50))

xi: $method choice relwage local riskaversion risk_miss $control i.store i.type_card i.type_cash ///
	if $sample & age_h==1 & mode_pref==1 & relwage>0, $option
outreg , $outopt2 merge(table) keep(relwage local riskaversion risk_miss $control) ///
	ctitle("VARIABLES", "Older workers")

outreg using "$work\Stata-out\cashiersurvey.doc" , $outopt2 replay(table) addtable replace ///
  title("Table 4. Accept cash payment") ///
  note("Sample includes cashiers who prefer card payment when wages are the same. Estimated by logistic regression with store and questionnaire-type fixed effects. Robust standard errors clustered by cashier (* p<0.1; ** p<0.05; *** p<0.01)")
 
outreg, clear(table)	
xi: $method choice relwage local riskaversion risk_miss $control i.store i.type_card i.type_cash ///
	if $sample & mode_pref==1 & relwage>0, $option
margins, dydx(*)	
outreg, stat(b_dfdx se_dfdx) plain coljust(lc) starloc(1) starlevels(10 5 1) bdec(3) ///
	summstat(N \ N_clust  \ r2_p) summtitles(Obs \ Cashiers  \ Pseudo R2) ///
	merge(table) keep(relwage local riskaversion risk_miss $control) ///
	ctitle("VARIABLES", "SME - All")    
	
xi: $method choice relwage local riskaversion risk_miss $control i.store i.type_card i.type_cash ///
	if $sample & age_h==1 & mode_pref==1 & relwage>0, $option
margins, dydx(*)	
outreg, stat(b_dfdx se_dfdx) plain coljust(lc) starloc(1) starlevels(10 5 1) bdec(3) ///
	summstat(N \ N_clust  \ r2_p) summtitles(Obs \ Cashiers  \ Pseudo R2) ///
	merge(table) keep(relwage local riskaversion risk_miss $control) ///
	ctitle("VARIABLES", "SME - Older workers")    

outreg using "$work\Stata-out\cashiersurvey.doc" , $outopt2 replay(table) addtable replace ///
  title("Table 4 (AME). Accept cash payment") 
 
*** Table 5. Wage premium ***  
use "$work\Stata\survey-public.dta" , clear
replace relwage = relwage / 10
keep if $sample

sum age, detail
gen age_h = (age >= r(p50))

//2 variables to indicating questionnaire types: from -3.5 to -0.5, or -4 to -1, and from 0.5 to 3.5, or 1 to 4
bysort cashier: egen minrelwage=min(relwage)
bysort cashier: egen maxrelwage=max(relwage)
gen type_card=0 
replace type_card=1 if minrelwage==-4
replace type_card=2 if minrelwage==-3.5
gen type_cash=0 
replace type_cash=1 if maxrelwage==4
replace type_cash=2 if maxrelwage==3.5

// Wage premium is 0 if indifferent between modes
gen min_relwage = 0 if mode_pref==2 
// Wage premium if cashier prefers cash and switches
by cashier: egen max1=max(relwage) if mode_pref==0 & choice==0
by cashier: egen max=min(max1) if mode_pref==0
// If cashier pefers cash never chooses card, set censored wage premium value as -5 for now.
by cashier: egen minchoice=min(choice) if mode_pref==0
by cashier: replace max=-5 if mode_pref==0 & minchoice==1
replace max=-5 if max==-3.5 & minchoice==1
replace min_relwage = max if mode_pref==0
drop max1 max minchoice
// Wage premium if cashier prefers card and switches
by cashier: egen min1=min(relwage) if mode_pref==1 & choice==1
by cashier: egen min=min(min1) if mode_pref==1
// If cashier pefers card never chooses cash, set censored set wage premium value as 5 for now.
by cashier: egen maxchoice=max(choice) if mode_pref==1
by cashier: replace min=5 if mode_pref==1 & maxchoice==0 
replace min=5 if min==3.5 & maxchoice==0 
replace min_relwage = min if mode_pref==1
drop min1 min maxchoice

collapse (first) store (first) mode_pref (mean) min_relwage local risk* $control age_h type_cash type_card, by(cashier) 

tab mode_pref //percentage preferring payment by card, cash, and indifferent
sum min_relwage , detail //median wage differential for switching

//intreg: lower end point (min_relwage_lower) and upper end point (min_relwage_upper)
gen min_relwage_lower=min_relwage-1 
gen min_relwage_upper=min_relwage
replace min_relwage_upper=. if min_relwage==5
replace min_relwage_lower=4 if min_relwage==5 & type_cash==1
replace min_relwage_lower=3.5 if min_relwage==5 & type_cash==2
replace min_relwage_lower=. if min_relwage==-5
replace min_relwage_upper=-4 if min_relwage==-5 & type_card==1
replace min_relwage_upper=-3.5 if min_relwage==-5 & type_card==2
replace min_relwage_lower=0 if min_relwage==0

//Sample: prefer card
outreg, clear(t4)
intreg min_relwage_lower min_relwage_upper local i.store i.type_card i.type_cash if mode_pref==1, vce(robust)
//fitstat displays the Cox-Snell pseudo-R2 
outreg , $outopt2 merge(t4) ///
	ctitle("VARIABLES", "Local")
intreg min_relwage_lower min_relwage_upper riskaversion risk_miss i.store i.type_card i.type_cash if mode_pref==1, vce(robust)
outreg , $outopt2 merge(t4) ///
	ctitle("VARIABLES", "Risk aversion")
intreg min_relwage_lower min_relwage_upper local riskaversion risk_miss $control i.store i.type_card i.type_cash if mode_pref==1, vce(robust)
outreg , $outopt2 merge(t4) ///
	ctitle("VARIABLES", "All")
intreg min_relwage_lower min_relwage_upper local riskaversion risk_miss $control i.store i.type_card i.type_cash if age_h==1 & mode_pref==1, vce(robust)
outreg , $outopt2 merge(t4) ///
	ctitle("VARIABLES", "Older workers")
	
outreg using "$work\Stata-out\cashiersurvey.doc" , $outopt replay(t4) addtable replace ///
  title("Table 5. Wage premium") ///
  note("Sample includes cashiers who prefer card payment when wages are the same. Estimated by interval regression with right censoring, store fixed effects, and questionnaire-type fixed effects. Robust standard errors.(* p<0.1; ** p<0.05; *** p<0.01)")


